/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modelo.Controler;

import Model.Beans.Institution;
import Model.Beans.User;
import Model.Servicios.InstSrv;
import Model.Utilities.Constantes.TypeUser;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;

/**
 *
 * @author John Mex
 */
public class AdminInstMgmtAction extends ActionSupport {

    Institution institution;
    User user;
    private String insIdToDelete;
    private InstSrv instSrv = new InstSrv();
    private List<Institution> institutions;

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }

    @Override
    public void validate() {
        if (getInstitution() != null) {
            if (institution.getName().equals("")) {
                addFieldError("institution.name", "Se requiere el nombre de la institución");
            }

            if (institution.getAddress().equals("")) {
                addFieldError("institution.address", "Se requiere la dirección de la institución");
            }

            if (institution.getPhone().equals("")) {
                addFieldError("institution.phone", "Se requiere el teléfono de la institución");
            } else if (!isValidPhoneField()) {
                addFieldError("institution.phone", "Se requieren 10 dígitos");
            }

            if (user.getUsername().equals("")) {
                addFieldError("user.username", "Se requiere el nombre de usuario de la institución");
            }

            if (user.getPassword().equals("")) {
                addFieldError("user.password", "Se requiere una contraseña");
            }
        }
    }

    private boolean isValidPhoneField() {
        String phoneNumber = institution.getPhone();
        boolean isValid = Pattern.matches("\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d", phoneNumber);
        return isValid;
    }

    public String addInstitution() {
        InstSrv instService = new InstSrv();
        this.getUser().setType(TypeUser.institution);
        this.getInstitution().setUser(this.getUser());
        if (!instService.usernameExist(this.getInstitution().getUser().getUsername())) {
            instService.addInstitution(institution);
            return SUCCESS;
        }
        return "institution_exist";

    }

    public Institution getInstitution() {
        return institution;
    }

    public void setInstitution(Institution institution) {
        this.institution = institution;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String delete() {
        System.out.println("InstitutionId1: " + insIdToDelete);
        instSrv.deleteInstitutionById(insIdToDelete);
        setInstitutions(instSrv.gellAll());
        return "delete";
    }

    public String getInsIdToDelete() {
        return insIdToDelete;
    }

    public void setInsIdToDelete(String insIdToDelete) {
        this.insIdToDelete = insIdToDelete;
    }

    public List<Institution> getInstitutions() {
        return institutions;
    }

    public void setInstitutions(List<Institution> institutions) {
        this.institutions = institutions;
    }
}
